import { createStore } from 'vuex'

export default createStore({
  state: {
    // 用户是否已经登录
    isLogin: (window.localStorage.getItem("isLogin")) == "true" ? Boolean(window.localStorage.getItem("isLogin")) : false,
    userInfo: (window.localStorage.getItem("userInfo")) ? JSON.parse(window.localStorage.getItem("userInfo")) : {},
    isRefresh: true
  },
  getters: {
    //用户信息
    userInfo(state) {
      return state.userInfo
    },
    //是否登录
    isLogin(state) {
      return state.isLogin
    }
  },
  mutations: {
    //登陆成功
    LoginSuccess(state, data) {
      state.isLogin = true
      window.localStorage.setItem("isLogin", true)
      state.userInfo = data
      console.log(data);
      localStorage.setItem('userInfo', JSON.stringify(data))
    },
    //退出登录
    LoginExit(state) {
      state.isLogin = false;
      state.userInfo = [];
      window.localStorage.setItem("isLogin", false);
      window.localStorage.removeItem("userInfo");
    },
    setNoRefresh(state) {
      state.isRefresh = false; //设置不刷新
    }
  },
  actions: {
    //向mutation提交登陆成功
    LoginSuccess({ commit }, data) {
      commit('LoginSuccess', data)
    },
    //退出登录
    LoginExit({ commit }) {
      commit('LoginExit')
    },
    setNoRefresh({ commit }) {
      commit('setNoRefresh')
    }
  },
  modules: {}
})
